package utils

import (
	"encoding/json"
	"github.com/pkg/errors"
	"io/ioutil"
)

//此文件存储全局数据信息


//城堡位置（士兵出生位置）
const (
	BronX1 = 54
	BronZ1 = -36
	BronX2 = -54
	BronZ2 = -35
	BronX3 = 53
	BronZ3 = 34
	BronX4 = -53
	BronZ4 = 35
	BronX5 = 55
	BronZ5 = 0
	BronX6 = -53
	BronZ6 = 0
	BronX7 = -2
	BronZ7 = -20
	BronX8 = -1
	BronZ8 = 21
	BronX9 = 18
	BronZ9 = 1
	BronX10 = -22
	BronZ10 = 1

)


//玩家状态
const (
	NotLogin = 1 //未登录
	Online   = 2 //在线
	Ready    = 3 //已准备
	Gaming   = 4 //游戏中
)

//技能类型
const (
	Damage = 1 		//范围伤害
	ReplyHp = 2 	//回血
	CallBoss = 3	//召唤
)

//技能信息
const (
	DamageValue1 = 80	//伤害
	DamageMagic1 = 50	//耗蓝

	DamageValue2 = 80	//伤害
	DamageMagic2 = 50	//耗蓝

	DamageValue3 = 80	//伤害
	DamageMagic3 = 50	//耗蓝

	ReplyHpValue1 = 65	//回血量
	ReplyHpMagic1 = 50	//耗蓝

	ReplyHpValue2 = 65	//回血量
	ReplyHpMagic2 = 50	//耗蓝

	ReplyHpValue3 = 65	//回血量
	ReplyHpMagic3 = 50	//耗蓝
)

const(
	UpgradeSoldierLevel = 1		//升级兵种
	UpgradeSkillLevel = 2		//升级技能点
)

//升级所需经验值
var ExpList = []int{0,90,98,105,113,448,476,504,532,560,1050,1100,1150,1200,1250,1300,1500}
const MaxUserLv = 16


//召唤Boss技能
const(
	BossAllHp1 = 450 //一级Boss血量
	BossAllHp2 = 550 //二级Boss血量
	BossAllHp3 = 750 //三级Boss血量

	BossAttackPowerLower1 = 36 //一级Boss攻击下限
	BossAttackPowerUpper1 = 40 //一级Boss攻击上限
	BossAttackPowerLower2 = 40 //二级Boss攻击下限
	BossAttackPowerUpper2 = 44 //二级Boss攻击上限
	BossAttackPowerLower3 = 50 //三级Boss攻击下限
	BossAttackPowerUpper3 = 60 //三级Boss攻击上限

	BossPrice = 300//Boss耗蓝

	BossDetectRange = 9
	BossAttackRange = 6
	BossAttackSpeed = 0.5
	BossMoveSpeed   = 0.15
)


//士兵状态
const (
	Static   = 1 //静止不动
	Moving   = 2 //移动中
	Fighting = 3 //战斗中
	Attacked = 4 //被攻击
	Chase    = 5 //追赶
	Death    = 6 //死亡
)

//士兵类型
const (
	Infantry = 1 //步兵
	Archer = 2 //弓兵
	Mage = 3 //骑兵
	Boss = 4 //超级怪物
)

//步兵信息
const (
	InfantryAllHp1 = 290 //一级步兵血量
	InfantryAllHp2 = 390 //二级步兵血量
	InfantryAllHp3 = 500 //三级步兵血量

	InfantryAttackPowerLower1 = 8 //一级步兵攻击下限
	InfantryAttackPowerUpper1 = 12 //一级步兵攻击上限
	InfantryAttackPowerLower2 = 12 //二级步兵攻击下限
	InfantryAttackPowerUpper2 = 16 //二级步兵攻击上限
	InfantryAttackPowerLower3 = 16 //三级步兵攻击下限
	InfantryAttackPowerUpper3 = 20 //三级步兵攻击上限

	InfantryPrice = 100 //步兵价格

	InfantryDetectRange = 9
	InfantryAttackRange = 6
	InfantryAttackSpeed = 0.5
	InfantryMoveSpeed   = 0.2
)

//弓兵信息
const (
	ArcherAllHp1 = 144 //一级弓兵血量
	ArcherAllHp2 = 200 //二级弓兵血量
	ArcherAllHp3 = 300 //三级弓兵血量

	ArcherAttackPowerLower1 = 22 //一级弓兵攻击下限
	ArcherAttackPowerUpper1 = 26 //一级弓兵攻击上限
	ArcherAttackPowerLower2 = 26//二级弓兵攻击下限
	ArcherAttackPowerUpper2 = 30 //二级弓兵攻击上限
	ArcherAttackPowerLower3 = 30 //三级弓兵攻击下限
	ArcherAttackPowerUpper3 = 34 //三级弓兵攻击上限

	ArcherPrice = 120 //弓兵价格

	ArcherDetectRange = 15
	ArcherAttackRange = 15
	ArcherAttackSpeed = 0.8
	ArcherMoveSpeed   = 0.2
)

//法师信息
const (
	MageAllHp1 = 200 //一级法师血量
	MageAllHp2 = 300 //二级法师血量
	MageAllHp3 = 450 //三级法师血量

	MageAttackPowerLower1 = 32 //一级法师攻击下限
	MageAttackPowerUpper1 = 36 //一级法师攻击上限
	MageAttackPowerLower2 = 36 //二级法师攻击下限
	MageAttackPowerUpper2 = 40 //二级法师攻击上限
	MageAttackPowerLower3 = 40 //三级法师攻击下限
	MageAttackPowerUpper3 = 44 //三级法师攻击上限

	MagePrice = 160 //法师价格

	MageDetectRange = 12
	MageAttackRange = 12
	MageAttackSpeed = 1
	MageMoveSpeed   = 0.2
)


//据点状态
const (
	Normal  = 1 //正常状态
	Occupy	= 2
)

//据点类型
const (
	Castle  = 1 	//城堡
	Village = 2 	//村庄
	MagicTower = 3	//魔法塔
)

//城堡信息
const (
	CastlePop = 3
	CastleMoneyAdd = 0
	CastleMagicAdd = 0
	CastleUpTo2Money = 500
	CastleUpTo3Money = 1000
	CastleOccupyMoney = 0
	CastleLandNum = 3
)

//村庄信息
const (
	VillagePop = 2
	VillageMoneyAdd = 0
	VillageMagicAdd = 0
	VillageUpTo2Money = 500
	VillageUpTo3Money = 1000
	VillageOccupyMoney = 150
	VillageLandNum = 2
)

//魔法塔信息
const (
	MagicTowerPop = 1
	MagicTowerMoneyAdd = 0
	MagicTowerMagicAdd = 2
	MagicTowerUpTo2Money = 500
	MagicTowerUpTo3Money = 1000
	MagicTowerOccupyMoney = 150
	MagicTowerLandNum = 1
)

//空地类型
const (
	Land = 4
	MagicStone = 2
	Farm = 1
	Mine = 3
)

//魔法石碑
const (
	MagicStoneMoney = 150
	MagicStoneMagicAdd = 1
)

//农场
const (
	FarmMoney = 100
	FarmPop = 1
)

//矿场
const (
	MineMoney = 100
	MineMoneyAdd = 2
)


//玩家信息
const (
	PlayerMoney    = 300
	PlayerMoneyAdd = 2
	PlayerMagic    = 50
	PlayerMagicAdd = 0
	PlayerPop      = 0
)

type GlobalObj struct {
	//IP地址
	IpAddress string
	//端口号
	Port int
	//连接池的最大连接数
	MaxConn int
	//一次读取数据包的最大长度
	MaxPackageSize uint32
	//worker工作池的Goroutine数量
	WorkerPoolSize uint32
	//每个work工作Goroutine对应消息队列的长度
	MaxWorkerTaskLen uint32
}

var GlobalObject *GlobalObj

//初始化全局对象
func GetGlobalObject() *GlobalObj {
	if GlobalObject == nil {
		//没有配置文件，使用默认值
		GlobalObject = &GlobalObj{
			IpAddress:        "0.0.0.0",
			Port:             8999,
			MaxConn:          1000,
			MaxPackageSize:   1024,
			WorkerPoolSize:   100,
			MaxWorkerTaskLen: 1024,
		}
		GlobalObject.Reload()
	}
	return GlobalObject
}

func (g *GlobalObj) Reload() (err error) {
	data, err := ioutil.ReadFile("conf/global.json")
	if err != nil {
		err = errors.WithStack(err)
		return
	}
	err = json.Unmarshal(data, &GlobalObject)
	if err != nil {
		err = errors.WithStack(err)
		return
	}
	return
}
